package games.eightPuzzle.heuristics;

import java.util.HashMap;

import games.eightPuzzle.states.*;

/**
 * @author avishl02
 * this class represents data structure who provides quick 
 * answers to the question in which coordinate the number X is
 */
/**
 * @author avishl02
 *
 */
public class CoordinatesHolder {
	
	private HashMap<Integer, Coordinate> _holder;
	
	//ctor
	public CoordinatesHolder(SlidingPuzzleState state){
		_holder = new HashMap<Integer, Coordinate>();
		
		for (int i=0;i<state.getNumRows();i++){
			for (int j=0;j<state.getNumCols();j++){
				_holder.put(state.get(i, j),new Coordinate(i,j));
			}
		}
	}
	

	/**
	 * gets the coordinate of the number num
	 * @param num
	 * @return
	 */
	public Coordinate get(int num){
		return _holder.get(num);
	}
	
	/**
	 * checks whether a given number is in a given row
	 * @param num
	 * @param i
	 * @return
	 */
	boolean isInRow(int num,int i){
		Coordinate c = _holder.get(num);
		return c!=null&& c.getI()==i;
	}
	
	/**
	 * checks whether a given number is in a given col
	 * @param num
	 * @param j
	 * @return
	 */
	boolean isInCol(int num,int j){
		Coordinate c = _holder.get(num);
		return c!=null&&c.getJ()==j;
	}

}
